// CustomStatic.cpp : implementation file
//

#include "stdafx.h"
#include "CustomStatic.h"


// CCustomStatic
CCustomStatic::CCustomStatic()
{
	m_pMemDestDC = NULL;
	m_pMemDestBitmap = NULL;
}

CCustomStatic::~CCustomStatic()
{
	if(m_pMemDestDC) {
		CBitmap* pOldBmp = (CBitmap*)m_pMemDestDC->SelectObject((CBitmap*)0);
		DeleteDC(m_pMemDestDC->GetSafeHdc());  delete m_pMemDestDC;  m_pMemDestDC = NULL;
	}
	if(m_pMemDestBitmap) { delete m_pMemDestBitmap;  m_pMemDestBitmap = NULL; }
}

/********************************************************************
 ********************************************************************
               Draw Background
********************************************************************
********************************************************************/
void CCustomStatic::DrawBackground()
{
	if(NULL == m_pMemBgDC) {
		CDialogExt* wndDlgex = (CDialogExt*)GetParent();
		m_pMemBgDC = wndDlgex->GetDialogCdc();

		GetWindowRect(m_rectWindow);  wndDlgex->ScreenToClient(&m_rectWindow);
	}
}

void CCustomStatic::DrawControl()
{
	if(NULL == m_pMemBgDC) return;

	if(NULL == m_pMemDestDC) {
		CDC* tmpDC = GetDC();
		m_pMemDestDC = new CDC();
		m_pMemDestDC->CreateCompatibleDC(tmpDC);
		m_pMemDestBitmap = new CBitmap();
		m_pMemDestBitmap->CreateCompatibleBitmap(tmpDC, m_rectWindow.Width(), m_rectWindow.Height());
		m_pMemDestDC->SelectObject(m_pMemDestBitmap);
		ReleaseDC(tmpDC);

		m_pMemDestDC->BitBlt(0, 0, m_rectWindow.Width(), m_rectWindow.Height(), m_pMemBgDC, m_rectWindow.left, m_rectWindow.top, SRCCOPY);
	}
}

void CCustomStatic::DrawString(CDC& dc)
{
	GetWindowTextW(m_strWindowText);
	if(!m_strWindowText.IsEmpty()){
		Graphics graphics(dc.GetSafeHdc());
		graphics.DrawString(m_strWindowText,
			-1,
			m_pFont,
			RectF((REAL)0,(REAL)0,(REAL)m_rectWindow.Width(),(REAL)m_rectWindow.Height()),
			&m_nStringFormat,
			m_pBrush);
	}
}

BEGIN_MESSAGE_MAP(CCustomStatic, CStatic)
	ON_WM_SIZE()
	ON_WM_PAINT()
END_MESSAGE_MAP()

// CCustomStatic message handlers

void CCustomStatic::OnSize(UINT nType, int cx, int cy)
{
	CStatic::OnSize(nType, cx, cy);
	ShowWindow(SW_HIDE);
	Invalidate();
	UpdateWindow();
	ShowWindow(SW_SHOW);
}

void CCustomStatic::PreSubclassWindow()
{
	// TODO: Add your specialized code here and/or call the base class
	ModifyStyle(0, BS_OWNERDRAW);
	CStatic::PreSubclassWindow();
}

void CCustomStatic::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CStatic::OnPaint() for painting messages

	DrawBackground();
	DrawControl();
	dc.BitBlt(0, 0, m_rectWindow.Width(), m_rectWindow.Height(), m_pMemDestDC, 0, 0, SRCCOPY);
	DrawString(dc);
}